package com.jpl.demo.mobility.ui.view.common;

import java.util.ArrayList;
import java.util.List;


import com.github.wolfie.refresher.Refresher;
import com.jpl.demo.mobility.business.enums.BusStopEnum;
import com.jpl.demo.mobility.business.service.impl.BusServiceImpl;
import com.jpl.demo.mobility.ui.util.SingleDataUpdaterThread;
import com.vaadin.addon.touchkit.ui.NavigationManager;
import com.vaadin.addon.touchkit.ui.NavigationManager.NavigationEvent;
import com.vaadin.addon.touchkit.ui.NavigationManager.NavigationEvent.Direction;
import com.vaadin.addon.touchkit.ui.NavigationManager.NavigationListener;
import com.vaadin.addon.touchkit.ui.NavigationView;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.VerticalLayout;

public abstract class AbstractTimingView extends NavigationView {

	private static final long serialVersionUID = 1L;

	private static final int SLEEP_TIME_IN_MILLIS = 30000;

	private List<SingleDataUpdaterThread> threadsToRefresh = new ArrayList<SingleDataUpdaterThread>();

	private VerticalLayout mainLayout;

	private boolean active;

	@SuppressWarnings("serial")
	public AbstractTimingView(final NavigationManager nav) {

		setWidth("100%");
		setHeight("100%");
		setCaption(getTimingViewName());

		this.mainLayout = new VerticalLayout();
		this.mainLayout.setWidth("100%");
		setContent(this.mainLayout);

		BusTimingToolBar toolBar = new BusTimingToolBar();
		setToolbar(toolBar);

		final Refresher refresher = new Refresher();
		refresher.setRefreshInterval(SLEEP_TIME_IN_MILLIS);
		refresher.setWidth("0px");
		refresher.setHeight("0px");
		this.mainLayout.addComponent(refresher);

		refresher.addListener(new Refresher.RefreshListener() {

			@Override
			public void refresh(Refresher source) {
				refreshTimes();
			}

		});

		toolBar.addRefreshButtonListener(new Button.ClickListener() {

			@Override
			public void buttonClick(ClickEvent event) {
				refreshTimes();
			}
		});

		nav.addListener(new NavigationListener() {

			@Override
			public void navigate(NavigationEvent event) {
				if (Direction.BACK.equals(event.getDirection())) {
					setActive(false);
				}
			}
		});

	}

	public synchronized void refreshTimes() {
		if (this.active) {
			for (SingleDataUpdaterThread thread : this.threadsToRefresh) {
				thread.run();
			}
		}
	}

	protected void addNewBusStopDetailsPanels(BusStopEnum busStop) {
		Panel panel = new Panel();
		HorizontalLayout panelLayout = new HorizontalLayout();
		panelLayout.setSizeFull();
		panelLayout.setSpacing(true);
		panelLayout.setMargin(false, true, false, true);
		panel.setContent(panelLayout);
		
		panel.addStyleName("padded");
		panel.setWidth("100%");
		panel.setCaption(busStop.getStopInformation());

		this.mainLayout.addComponent(panel);

		SingleDataUpdaterThread thread = new SingleDataUpdaterThread(BusServiceImpl.getInstance(), busStop, panel);
		this.threadsToRefresh.add(thread);
	}

	protected void refresh(Refresher source) {

	}

	public abstract String getTimingViewName();

	public boolean isActive() {
		return this.active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

}
